﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace 书易.DAL
{
    public class DbHelper
    {
        /// <summary>
        /// 静态块
        /// </summary>
        static DbHelper()
        {
            try
            {
                ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            }
            catch
            {
                throw new Exception("没有配置数据库连接字符串，请检查 App.config/Web.config！");
            }
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string ConnectionString { get; set; }

        /// <summary>
        /// 执行增删改操作
        /// </summary>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                return cmd.ExecuteNonQuery();
            }
        }

        /// <summary>
        /// 执行单值查询
        /// </summary>
        public static object ExecuteScalar(string sql, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                return cmd.ExecuteScalar();
            }
        }

        /// <summary>
        /// 执行连接式查询 Reader查询
        /// </summary>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            if (pars != null)
                cmd.Parameters.AddRange(pars);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        /// <summary>
        /// 执行断开式查询 DataTable查询
        /// </summary>
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            if (pars != null)
                sda.SelectCommand.Parameters.AddRange(pars);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 执行断开式查询 DataSet查询
        /// </summary>
        public static DataSet ExecuteDataSet(string sql, params SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            if (pars != null)
                sda.SelectCommand.Parameters.AddRange(pars);
            DataSet dt = new DataSet();
            sda.Fill(dt);
            return dt;
        }
    }
}
